//
//  DataManager.m
//  FileManager

//
//  Created by humingxing on 14-7-25.
//  Copyright (c) 2014年 HMX. All rights reserved.
//

#import "DataManager.h"
@implementation DataManager
//-(void)aa
//{
//    //对于错误信息
//    NSError *error;
//    NSFileManager *fileMgr = [NSFileManager defaultManager];
//  
//    
//    
//    //显示文件目录的内容
//    NSLog(@"Documentsdirectory: %@",
//          [fileMgr contentsOfDirectoryAtPath:[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"] error:&error]);
//    
//    
//    
//    //或者
//    //NSData *data1 = [NSData dataWithContentOfPath:[DataManager creatFilePath:@"file1"]];
//    NSData *data2=[NSData dataWithContentsOfFile:[DataManager creatFilePath:@"file1"]];
//    NSString *str1=[[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding];
//    NSLog(@"%@",str1);
//}

+(void)writeStringToFile:(NSString *)str atPathName:(NSString *)pathStr
{
    //写入文件
    [str writeToFile:[DataManager creatFilePath:pathStr] atomically:YES
            encoding:NSUTF8StringEncoding error:nil];
}
+(void)writeNSMutableArrayToFile:(NSMutableArray *)array atPathName:(NSString *)pathStr
{
    [array writeToFile:[DataManager creatFilePath:pathStr] atomically:YES];
}
+(void)writeArrayToFile:(NSArray *)array atPathName:(NSString *)pathStr
{
    [array writeToFile:[DataManager creatFilePath:pathStr] atomically:YES];
}
+(void)writeMutableDictionaryToFile:(NSMutableDictionary *)dictionary atPathName:(NSString *)pathStr
{
    [dictionary writeToFile:[DataManager creatFilePath:pathStr] atomically:YES];
}
+(void)writeDictionaryToFile:(NSDictionary *)dictionary atPathName:(NSString *)pathStr
{
    [dictionary writeToFile:[DataManager creatFilePath:pathStr] atomically:YES];
}
+(void)writeDataToFile:(NSData *)data atPathName:(NSString *)pathStr
{
    [data writeToFile:[DataManager creatFilePath:pathStr] atomically:YES];
}
//返回nsstring类型数据
+(NSString *)readStringAtPathString:(NSString *)pathStr
{
    NSString *str1=[[NSString alloc]initWithContentsOfFile:[DataManager creatFilePath:pathStr] encoding:NSUTF8StringEncoding error:nil];
    return str1;
}
+(NSMutableArray *)readMutableArrarAtPathString:(NSString *)pathStr
{
    NSMutableArray *array=[[NSMutableArray alloc]initWithContentsOfFile:[DataManager creatFilePath:pathStr]];
    return array;
}
+(NSArray *)readArrayAtPathString:(NSString *)pathStr
{
    NSArray *array=[[NSArray alloc]initWithContentsOfFile:[DataManager creatFilePath:pathStr]];
    return array;
}

+(NSMutableDictionary *)readNSMutableDictionaryAtPathString:(NSString *)pathStr
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithContentsOfFile:[DataManager creatFilePath:pathStr]];
    return dic;
}
+(NSDictionary *)readDictionaryAtPathString:(NSString *)pathStr
{
    NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:[DataManager creatFilePath:pathStr]];
    return dic;
}
+(NSData *)readDataAtPathString:(NSString *)pathStr
{
    NSData *data=[NSData dataWithContentsOfFile:[DataManager creatFilePath:pathStr]];
    return data;
}
+(BOOL)deleteFileAtPath:(NSString *)pathStr
{
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager removeItemAtPath:[DataManager creatFilePath:pathStr] error:&error] != YES)
    {
        NSLog(@"Unable to delete file: %@", [error localizedDescription]);
        return NO;
    }
    else
        return YES;
}
+(NSString *)creatFilePath:(NSString*)str
{
    NSString * fileName = [[str componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet]invertedSet]] componentsJoinedByString:@""];
    //创建文件夹：
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[documentDirectories objectAtIndex:0] stringByAppendingPathComponent:@"Data"];
    
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath= [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.ACData",fileName]];
    return filePath;
    
    /*//如果不需要文件夹 可以使用此代码
     //指向文件目录
     NSString *documentsDirectory= [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
     NSString *filePath= [documentsDirectory
     stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",str]];
     return filePath;
     */
}

@end
